﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace VisualMove
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class PrintQRCode : Page
    {
        public PrintQRCode()
        {
            List<string> oList = new List<string>(oLabels);
            oList.Sort(); // Alphabetize the list
            oStandardQRCodeLabels = new ObservableCollection<string>(oList);

            this.InitializeComponent();

            DataTransferManager oTransferManager = DataTransferManager.GetForCurrentView();
            oTransferManager.DataRequested += oTransferManager_DataRequested;
        }

        void oTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequest oRequest = args.Request;
            oRequest.Data.Properties.Title = "Test";
            oRequest.Data.Properties.Description = "Test QR Image";
            InMemoryRandomAccessStream oMemoryStream = new InMemoryRandomAccessStream();
            QRImage.PixelBuffer.AsStream().CopyTo(oMemoryStream.AsStreamForWrite());
            
            oRequest.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(oMemoryStream));
        }

        public WriteableBitmap QRImage
        {
            get;
            private set;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            BarcodeWriter oWriter = new BarcodeWriter();
            oWriter.Format = BarcodeFormat.QR_CODE;
            oWriter.Options.Height = 256;
            oWriter.Options.Width = 256;
            QRImage = oWriter.Write("Test");

            DataContext = this;
        }

        public ObservableCollection<string> oStandardQRCodeLabels
        {
            get;
            set;
        }

        private string[] oLabels = { "Garage",
                                           "Basement",
                                           "Kitchen",
                                           "Shed", 
                                           "Living Room", 
                                           "Bathroom #1", 
                                           "Bathroom #2",
                                           "Bathroom #3",
                                           "Family Room", 
                                           "Dining Room", 
                                           "Foyer", 
                                           "Master Bedroom",
                                           "Bedroom #1", 
                                           "Bedroom #2",
                                           "Bedroom #3", 
                                           "Bedroom #4", 
                                           "Bedroom #5"
                                         };

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
